package controller;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import json.JSONCustomer;
import json.JSONParam;
import json.JSONResponse;
import vo.Customer;
import vo.DataTableReturnObject;
import service.CustomerService;

@Controller
@RequestMapping("/customerInfo")
public class CustomerInfoController extends JSONController {
	/*
	 * 用@Autowired注解属性，就不用写set方法了
	 * JSR-250标准注解，推荐使用@Resource来代替Spring专有的@Autowired注解。
	 */
	@Resource
	private CustomerService customerService;

	@Autowired
	public CustomerInfoController(Validator validator) {
		super(validator);
	}
	
//	@Autowired
//	public void setCustomerService(CustomerService service) {
//		this.customerService = service;
//	}
	
	@RequestMapping(value = "/new", method = RequestMethod.POST)
	@ResponseBody
	public JSONResponse add(@RequestBody JSONCustomer jsoncustomer) throws IllegalAccessException, InvocationTargetException {
		System.out.println("添加之前");
		System.out.println(jsoncustomer);
		System.out.println(jsoncustomer.toString());
		Set<ConstraintViolation<JSONCustomer>> failures = validator.validate(jsoncustomer);
		System.out.println("添加之后1");
		if (failures.isEmpty()) {
			Customer customer = new Customer();
			BeanUtils.copyProperties(customer, jsoncustomer);
			System.out.println("添加之后2");
			customerService.addCustomer(customer);
			System.out.println("添加之后3");
			BeanUtils.copyProperties(jsoncustomer, customer);
			System.out.println("添加之后4");
			return successed(jsoncustomer);
		} else {
			return failed(failures);
		}
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.POST)
	@ResponseBody
	public JSONResponse search(@RequestBody JSONParam[] params) throws IllegalAccessException, InvocationTargetException {
		HashMap<String, String> paramMap = convertToMap(params);
		String sEcho = paramMap.get("sEcho");
		String customerName = paramMap.get("customerName");
		int start = Integer.parseInt(paramMap.get("iDisplayStart"));
		int length = Integer.parseInt(paramMap.get("iDisplayLength"));
		
		List<Object> customerList = customerService.search(customerName, start, length);
		Long count = (Long)customerList.get(0);
		System.out.println("customer结果数："+count);
		int record = customerList.size() - 1;
		String[][] data = new String[record][];
		for(int i=0; i<record; i++) {
			Customer customer = (Customer)customerList.get(i+1);
			JSONCustomer jsonCustomer = new JSONCustomer();
			BeanUtils.copyProperties(jsonCustomer, customer);
			data[i] = jsonCustomer.toArray();
		}
		
		return successed(new DataTableReturnObject(count.longValue(), count.longValue(), sEcho, data));
	}
	

}
